package com.examiner.service.impl;

import com.examiner.bean.CommonPerson;
import com.examiner.bean.Department;
import com.examiner.dao.CommonPersonDao;
import com.examiner.dao.DepartmentDao;
import com.examiner.page.QueryInfo;
import com.examiner.service.CommonPersonService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

@Service
public class CommonPersonServiceImpl implements CommonPersonService {
    @Resource
    private CommonPersonDao commonPersonDao;

    @Resource
    private DepartmentDao departmentDao;

    /**
     * 检测该用户名是否存在
     * @param commonPersonUserName
     * @return
     */
    @Override
    public CommonPerson hasUserName(String commonPersonUserName) {
        return commonPersonDao.findByUserName(commonPersonUserName);
    }

    /**
     * 根据用户名查信息列表
     * @param queryInfo
     * @return
     */
    @Override
    public List<CommonPerson> findByUserName(QueryInfo queryInfo) {
        // 新建Specification
        Specification spec = new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery cq, CriteriaBuilder cb) {
                Path commonPersonUseName = root.get("useName");
                Predicate like = cb.like(commonPersonUseName.as(String.class), "%" + queryInfo.getQuery() + "%");
                return like;
            }
        };
        // 新建pageable
        Pageable pageable = PageRequest.of(queryInfo.getPageNum(), queryInfo.getPageSize());
        Page page = commonPersonDao.findAll(spec, pageable);
        return page.getContent();
    }

    /**
     * 添加一般用户
     * @param commonPerson
     */
    @Override
    public CommonPerson addCommonPerson(CommonPerson commonPerson) {
        return commonPersonDao.save(commonPerson);
    }

    /**
     * 删除
     * @param commonPersonId
     */
    @Override
    public Boolean removeCommonPerson(Integer commonPersonId) {
        CommonPerson commonPerson = commonPersonDao.getOne(commonPersonId);
        removeUser(commonPerson.getUserName());
        removeDepartment(commonPerson.getUserName());
        commonPersonDao.deleteById(commonPersonId);
        return commonPersonDao.existsById(commonPersonId);
    }

    /**
     * 删除用户
     * @param superior
     */
    public void removeUser(String superior){
        List<CommonPerson> commonPersonList = commonPersonDao.findBySuperior(superior);
        if (commonPersonList == null) return;
        for (CommonPerson commonPerson : commonPersonList) {
            removeUser(commonPerson.getUserName());
            removeDepartment(commonPerson.getUserName());
            commonPersonDao.delete(commonPerson);
        }
    }

    /**
     * 删除部门
     * @param superior
     */
    public void removeDepartment(String superior){
        List<Department> departmentList = departmentDao.findBySuperior(superior);
        if (departmentList == null) return;
        for (Department department : departmentList) {
            removeUser(department.getUserName());
            departmentDao.delete(department);
        }
    }

    /**
     * 根据id判断是否存在
     * @param commonPersonId
     * @return
     */
    @Override
    public Boolean existsById(Integer commonPersonId) {
        return commonPersonDao.existsById(commonPersonId);
    }

    /**
     * 动态删除
     * @param commonPersonId
     * @param state
     */
    @Transactional
    @Override
    public void changeCommonPersonState(Integer commonPersonId, Boolean state) {
        commonPersonDao.changeCommonPersonState(commonPersonId,state);
    }

    /**
     * 修改信息
     * @param commonPerson
     */
    @Override
    public CommonPerson changeCommonPersonInfo(CommonPerson commonPerson) {
        return commonPersonDao.save(commonPerson);
    }

    /**
     * 根据id 查询信息
     * @param commonPersonId
     * @return
     */
    @Override
    public CommonPerson findById(Integer commonPersonId) {
        return commonPersonDao.getOne(commonPersonId);
    }


    @Override
    @Transactional
    public Boolean updateManagerPassword(Integer id, String encodePsw) {
        int i = commonPersonDao.updatePassword(id, encodePsw);
        System.out.println(i);
        if (i!= 1) return false;
        return true;
    }

    /**
     * 根据 上级 获取 用户列表
     * @param superior
     * @return
     */
    @Override
    public List<CommonPerson> getPersonList(String superior) {
        System.out.println(superior);
        return commonPersonDao.findBySuperior(superior);
    }

    /**
     * 获取所有下级用户及不门
     * @param superior
     * @return
     */
    @Override
    public List<Object> getAllPersonList(String superior) throws Exception {
        List<Object> users = new ArrayList<>();;
       try{
           getAll(superior , users);
          /* List<CommonPerson> commonPersonList = commonPersonDao.findBySuperior(superior);
           List<Department> departmentList = departmentDao.findBySuperior(superior);
           if (departmentList.size() != 0){
               users.addAll(departmentList);
               for (Department department : departmentList) {
                   List<CommonPerson> bySuperior = commonPersonDao.findBySuperior(department.getUserName());
                   if (bySuperior.size() != 0 ) {
                       for (CommonPerson commonPerson : bySuperior) {
                           commonPerson.setPassword("");
                           users.add(commonPerson);
                       }
                   }
               }
           }
           if (commonPersonList.size() != 0){
               for (CommonPerson commonPerson : commonPersonList) {
                   commonPerson.setPassword("");
                   users.add(commonPerson);
                   getAllPersonList(commonPerson.getUserName());
               }
           }*/
       }catch (Exception e){e.printStackTrace();}
        return users;
    }

    /**
     * 恢复所有状态
     */
    @Transactional
    @Override
    public void changeAllState() throws Exception {
        try{
            commonPersonDao.changeAllState(false);
        }catch (Exception e){e.printStackTrace();}
    }

    @Transactional
    @Override
    public void changeExaminer(CommonPerson commonPerson) throws Exception {
        commonPersonDao.changeCommonPersonExaminer(commonPerson.getId(),commonPerson.getEid(),commonPerson.getExaminer());
    }

    /**
     * 根据部门查用户
     * @param sid
     * @param stype
     * @return
     */
    @Override
    public List<CommonPerson> findBySidAndStype(Integer sid, String stype) {
        return commonPersonDao.findBySIdAndStype(sid, stype);
    }

    public void getAll(String superior, List<Object> users) {
            List<CommonPerson> commonPersonList = commonPersonDao.findBySuperior(superior);
            List<Department> departmentList = departmentDao.findBySuperior(superior);
            if (departmentList.size() != 0){
                users.addAll(departmentList);
                for (Department department : departmentList) {
                    List<CommonPerson> bySuperior = commonPersonDao.findBySuperior(department.getUserName());
                    if (bySuperior.size() != 0 ) {
                        for (CommonPerson commonPerson : bySuperior) {
                            commonPerson.setPassword("");
                            users.add(commonPerson);
                        }
                    }
                }
            }
            if (commonPersonList.size() != 0){
                for (CommonPerson commonPerson : commonPersonList) {
                    commonPerson.setPassword("");
                    users.add(commonPerson);
                    getAll(commonPerson.getUserName(),users);
                }
            }
    }

}
